package com.example.waibao.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

public class material_Util {
    //阿里域名
    public static final String ALI_DOMAIN = "https://czh0123-text.oss-cn-hangzhouzhou.aliyuncs.com/";

    public static String uploadImage(MultipartFile file) throws IOException {
        //生成文件名
        String originalFilename = file.getOriginalFilename();//原来的图片
        String ext = "." + FilenameUtils.getExtension(originalFilename);
        String uuid = UUID.randomUUID().toString().replace("-","");
        String filename = uuid+ext;

        //地域节点
        String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
        String accessKeyId = "LTAI5tARrUEbvP5pKtoWFKvT";
        String accessKeySecret = "jXmFaGnQD53Xj5MJ34u6iuNio69oVD";
        String bucketName = "tiebingice";

        //OSS客户端对象
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(
                bucketName, //存储空间名称
                filename, //文件名称
                file.getInputStream() //文件输入流
        );
        String url = endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + filename;
        ossClient.shutdown();

        return url;
//        return ALI_DOMAIN+filename;


    }
}
